package com.fbty.mt.dwr;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.fbty.base.dwr.BaseDwr;
import com.fbty.base.dwr.DwrAuthHelper;
import com.fbty.mt.domain.User;
import com.fbty.mt.service.LoginService;

public class LoginDwr {
	private LoginService loginService;
	private User user;
	public String checkPwd(String username,String password,String code) {
		String scode = (String)DwrAuthHelper.getSession().getAttribute("code");
		if(!code.toUpperCase().equals(scode)){
			return "验证码错误!";
		}
		if (username == null || password == null || username.equals("") || password.equals("")) {
			return "帐号或密码错误!";

		} else if (username.indexOf(" ") != -1 || password.indexOf(" ") != -1) {
			return "帐号或密码错误!";
		} else {
			if (loginService.findUsername(username).size() <= 0) {
				return "帐号或密码错误!";
			} else {
				List<User> list = loginService.findUsernamePassword(username, password);
				if (list.size() <= 0) {
					return "帐号或密码错误!";
				}
				user=list.get(0);
			}
		}
		DwrAuthHelper.getSession().setAttribute("id", user.getId());
		DwrAuthHelper.getSession().setAttribute("user", user);
		return "ok";
	}
	
	/**
	 * 验证注册用户是否存在
	 * @param loginname
	 * @return
	 */
	public boolean validLogin(String loginname) {
		return loginService.validLogin(loginname);
	}
	public void logout() {
		DwrAuthHelper.getSession().removeAttribute("user");
		DwrAuthHelper.getSession().removeAttribute("shoppingcar");
		DwrAuthHelper.getSession().removeAttribute("num");
		DwrAuthHelper.getSession().removeAttribute("info");
	}
	
	public LoginService getLoginService() {
		return loginService;
	}

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	
	/**
	 * 
	 * Description: 判断用户session是否过期<br>
	 * @return
	 */
	public boolean userFlag(){
		User user = (User)DwrAuthHelper.getSession().getAttribute("user");
		if(user!=null){
			return true;
		}
		return false;
	}
	
}
